<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\request\LoginRequest;
use app\admin\service\LoginService;
use app\BaseController;
use thans\jwt\facade\JWTAuth;

class Login extends BaseController
{
    /**
     * 登录
     * User Reborn
     * Time 2020/4/3 15:46
     * @throws \app\common\exception\Http
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function login(LoginService $loginService,LoginRequest $loginRequest)
    {
        $loginRequest->scene('check')->validate(input());
        $info=$loginRequest->requestData(input(),'data');

        switch ($info['type']){
            case 1:
                $user=$loginService->login($info['login_name'],$info['password']);
                break;
            default:
                break;
        }

        $user['token']=$loginService->makeToken($user);

        $this->ajaxSuccess($user);
    }

    /**
     * 退出登录
     * User Reborn
     * Time 2020/4/3 15:45
     * @throws \app\common\exception\Http
     */
    public function logout()
    {
        $token=$this->request->header('authorization');
        $token=trim(str_replace('Bearer',' ',$token),' ');
        JWTAuth::invalidate($token);
        $this->ajaxSuccess($token);
    }

    /**
     * 刷新token
     * User Reborn
     * Time 2020/4/3 15:45
     */
    public function refreshToken()
    {
        $token=JWTAuth::refresh();
        $this->ajaxSuccess($token);
    }

}
